package com.noble.meether.chat.ui.agora;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;

import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.glamour.module.logic.socket.AutoConnectMgr;
import com.noble.glamour.module.util.UIShow;
import com.noble.library.log.PToast;
import com.noble.meether.R;
import com.noble.meether.chat.ui.agora.act.RtcGroupInitAct;
import com.noble.meether.chat.ui.agora.act.RtcInitActivity;
import com.noble.meether.chat.ui.agora.act.comment.RtcCommentActivity;
import com.noble.meether.chat.ui.agora.act.dialog.RtcFaceUnityDialog;
import com.noble.meether.chat.ui.agora.act.dialog.black.RtcBlackDlg;
import com.noble.meether.chat.ui.agora.act.dialog.report.RtcVideoCommentDlg;
import com.noble.meether.chat.ui.agora.act.robvideo.RtcRobVideoActivity;
import com.noble.meether.chat.ui.agora.act.video.RtcVideoActivity;
import com.noble.meether.chat.ui.agora.act.voice.RtcVoiceActivity;
import com.noble.meether.chat.ui.agora.dialog.RtcBottomGiftDialog;
import com.noble.meether.chat.ui.agora.dialog.listener.GiftSendListener;
import com.noble.meether.chat.ui.agora.model.AgoraConstant;
import com.noble.meether.chat.ui.agora.util.RtcJointChannel;
import com.noble.meether.chat.util.ChatMediaPlayer;

/**
 * 音视频聊天相关页面
 */
public class RtcUIShow extends UIShow {

    /**
     * 男性：群发等待接通
     */
    public static void showRtcGroupInitAct(final Activity activity) {
        AutoConnectMgr.getInstance().sendHeartData(); // 发送心跳，检测TCP连接状态
        if (ChatMediaPlayer.getInstance().isPlaying()) {
            ChatMediaPlayer.getInstance().stopPlayVoice();
        }
        Intent intent = new Intent(activity, RtcGroupInitAct.class);
        activity.startActivity(intent);
    }

    /**
     * 邀请，受邀，音频通信界面
     */
    public static void showRtcInitAct(Context context) {
        ModuleMgr.getRtcEnginMgr().getEngineConfig().isBusy = true;
        AutoConnectMgr.getInstance().sendHeartData(); // 发送心跳，检测TCP连接状态
        if (ChatMediaPlayer.getInstance().isPlaying()) {
            ChatMediaPlayer.getInstance().stopPlayVoice();
        }
        showActivityFromBack(RtcInitActivity.class);
    }

    /**
     * 视频聊天页
     */
    public static void showRtcVideoAct(Activity activity) {
        showRtcVideoAct(activity, false);
    }

    // 不带权限检测
    public static void showRtcVideoActNoCheck(Activity activity) {
        ModuleMgr.getRtcEnginMgr().getEngineConfig().isBusy = true;
        activity.startActivity(new Intent(activity, RtcVideoActivity.class));
    }

    public static void showRtcVideoAct(final Activity activity, final boolean isFinish) {
        showRtcVideoActNoCheck(activity);
        if (isFinish) activity.finish();
    }

    /**
     * 抢聊视频聊天页
     */
    public static void showRtcRobVideoAct(Activity activity) {
        ModuleMgr.getRtcEnginMgr().getEngineConfig().isBusy = true;
        RtcJointChannel.getInstance().joinChannelPrepare(false);
        activity.startActivity(new Intent(activity, RtcRobVideoActivity.class));
    }

    /**
     * 打开通话中界面
     */
    public static void showRtcVoiceAct(Activity activity) {
        showRtcVoiceAct(activity, false);
    }

    public static void showRtcVoiceAct(final Activity activity, final boolean isFinish) {
        ModuleMgr.getRtcEnginMgr().getEngineConfig().isBusy = true;
        activity.startActivity(new Intent(activity, RtcVoiceActivity.class));
        if (isFinish) activity.finish();
    }

    /**
     * 视频评论页面
     */
    public static void showRtcCommentAct(final Context activity, long tuid) {
        ModuleMgr.getCenterMgr().reqMyInfo("音视频通话结束更新");
        // 只有视频时弹评价结算页面
        if (ModuleMgr.getRtcEnginMgr().getEngineConfig().mChatType != AgoraConstant.RTC_CHAT_VIDEO) {
            return;
        }

        // 女性不做处理
        if (!ModuleMgr.getCenterMgr().getMyInfo().isMan()) {
            return;
        }
        ModuleMgr.getCommonMgr().getUserRelation(tuid, (isOk, status) -> {
            if (!isOk) {
                return;
            }

            if (status == 1 || status == -2) return;
            Intent intent = new Intent(activity, RtcCommentActivity.class);
            activity.startActivity(intent);
        });
    }

    /**
     * 送礼物底部弹框
     *
     * @param to_id   他人id
     * @param isCheck 是否进行音视频通话检测
     */
    public static void showRtcBottomGiftDlg(final Context context, final long to_id, boolean isCheck, final GiftSendListener listener) {
        if (ModuleMgr.getCommonMgr().getGiftLists().getGiftList().size() > 0) {
            showRtcGiftDlg(context, to_id, isCheck, listener);
            return;
        }
        ModuleMgr.getCommonMgr().requestGiftList(isOk -> {
            if (!isOk) {
                PToast.showShort(context.getString(R.string.net_error_retry));
                return;
            }

            if (ModuleMgr.getCommonMgr().getGiftLists().getGiftList().size() <= 0)
                return;
            showRtcGiftDlg(context, to_id, isCheck, listener);
        });
    }

    private static void showRtcGiftDlg(Context context, long to_id, boolean isCheck, GiftSendListener listener) {
        RtcBottomGiftDialog rtcDialog = new RtcBottomGiftDialog();
        rtcDialog.setToId(to_id);
        rtcDialog.setIsCheck(isCheck);
        rtcDialog.setCtx(context);
        rtcDialog.setSendGiftListener(listener);
        rtcDialog.showDialog((FragmentActivity) context);
    }

    /**
     * 男用户：视频中投诉弹框
     */
    public static void showRtcVideoCommentDlg(final FragmentActivity context) {
        RtcVideoCommentDlg dialog = new RtcVideoCommentDlg();
        dialog.showDialog(context);
    }

    /**
     * 视频中加入黑名单弹框
     */
    public static void showRtcBlackDlg(final FragmentActivity context, long uidTo) {
        RtcBlackDlg dialog = new RtcBlackDlg(uidTo);
        dialog.showDialog(context);
    }

    /**
     * FaceU道具弹框
     */
    public static void showRtcFaceUnityDialog(final FragmentActivity context) {
        RtcFaceUnityDialog dialog = new RtcFaceUnityDialog();
        dialog.showDialog(context);
    }

}
